using UnityEngine;
using MinesweeperPlus.Scripts.Abstractions.Interfaces;

namespace MinesweeperPlus.Scripts.Objects
{
    public sealed class PlayerCamera : IGameObject
    {
        #region Variables
        private GameObject _gameObject;
        private Camera _camera;
        #endregion
        #region Properties
        public Transform Transform { get; private set; }
        public bool IsActive 
        {
            get => _gameObject.activeSelf;
            set => _gameObject.SetActive(value);
        }
        
        public Vector2 ZoomRange { get; set; }
        public Rect PositionRange { get; set; }
        public Color BackgroundColor
        {
            get => _camera.backgroundColor;
            set => _camera.backgroundColor = value;
        }
        public float Zoom
        {
            get => _camera.orthographicSize;
            set 
            {
                _camera.orthographicSize = Mathf.Clamp(value, ZoomRange.x, ZoomRange.y);
            }
        }
        public Vector3 Position
        {
            get => Transform.position;
            set
            {
                var newPosition = new Vector3(
                    Mathf.Clamp(value.x, PositionRange.x, PositionRange.x + PositionRange.width),
                    Mathf.Clamp(value.y, PositionRange.y, PositionRange.y + PositionRange.height),
                    value.z);
                Transform.position = newPosition;
            }
        }
        #endregion

        #region PlayerCamera Methods
        public PlayerCamera(Camera camera)
        {
            _camera = camera;
            _gameObject = _camera.gameObject;
            Transform = _camera.transform;
        }

        public Vector3 ScreenToWorldPoint(Vector3 screePoint) =>
            _camera.ScreenToWorldPoint(screePoint);

        public void Destroy()
        {
            GameObject.Destroy(_gameObject);
        }
        #endregion
    }
}
